<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<html>
<head>
	<title>服务网点管理</title>
	<script>
		$(document).ready(function() {
			var arrLink=$("a[id^='detail_']");
			//查找所属区域
			$(arrLink).each(function(){
				$(this).click(function(){
					var id = $(this).attr("name");
					$.ajax({
						type : 'post',
						url : '${ctx}/areaGroup/detailArea',
						data : 'id='+ id,
						dataType : 'json',
						success : function(data) {
							var areas = data.areas;
							$("#areas_detail_"+id).html("");
							$("#areas_detail_"+id).append("<tr><th>省/直辖市</th><th>市/县</th></tr>");
							$.each(areas, function(i) {
								var str = "";
								if(areas[i].areaType == 2){
									str += "<tr><td>" + areas[i].areaName + "</td>";
									str += "<td>";
									var j;
									for(j in areas){
										if(areas[j].areaType == 3 && areas[i].id == areas[j].area.id){
											str += areas[j].areaName + " | ";
										}
									}
									str += "</td></tr>";
								}
								//alert(str);
								$("#areas_detail_"+id).append(str);
							});
						},
						error : function() {
						}
					});
				});
    		});
		});
		
		$(document).ready(function() {
			var arrLinkPipe=$("a[id^='detailPipe_']");
			//查找所属流水线
			$(arrLinkPipe).each(function(){
				$(this).click(function(){
					var id = $(this).attr("name");
					$.ajax({
						type : 'post',
						url : '${ctx}/areaGroup/detailPipe',
						data : 'id='+ id,
						dataType : 'json',
						success : function(data) {
							var pmsPipelines = data.pmsPipelines;
							$("#pmsPipelines_detailPipe_"+id).html("");
							var str = "<tr><th>流水线名称</th><th>类型</th><th>状态</th></tr>"
							$.each(pmsPipelines, function(i) {
								str += "<tr><td>" + pmsPipelines[i].pipelineName + "</td>";
								
								str	+= "<td>";
								if(pmsPipelines[i].pipelineType == 0){
									str += "单件";
								}
								if(pmsPipelines[i].pipelineType == 1){
									str += "多件";
								}
								str += "</td>";
								
								str	+= "<td>";
								if(pmsPipelines[i].status == "working"){
									str += "运转中";
								}
								if(pmsPipelines[i].status == "idle"){
									str += "闲置中";
								}
								if(pmsPipelines[i].status == "delete"){
									str += "已废除";
								}
								str += "</td></tr>";
							});
							$("#pmsPipelines_detailPipe_"+id).append(str);
						},
						error : function() {
						}
					});
				});
    		});
		});
		$(document).ready(function() {
			var arrLinkBatch=$("a[id^='detailBatch_']");
			//查找所属批次
			$(arrLinkBatch).each(function(){
				$(this).click(function(){
					var id = $(this).attr("name");
					$.ajax({
						type : 'post',
						url : '${ctx}/areaGroup/detailBatch',
						data : 'id='+ id,
						dataType : 'json',
						success : function(data) {
							var areaGroupFindBatchs = data.areaGroupFindBatchs;
							$("#shippedBatch_detailBatch_"+id).html("");
							var str = "";
							str += "<tr><th>出库批次编号</th><th>WMS出库批次编号</th><th>出货时间</th><th>商家编码</th><th>状态</th></tr>";
							$.each(areaGroupFindBatchs, function(i) {
								str += "<td>" + areaGroupFindBatchs[i].batchCode + "</td>"
									+"<td>" + areaGroupFindBatchs[i].wmsCode + "</td>"
									+"<td>" + areaGroupFindBatchs[i].shippedTime + "</td>"
									+"<td>" + areaGroupFindBatchs[i].customerCode + "</td>";
								
								str	+= "<td>";
								if(areaGroupFindBatchs[i].status == "shipped"){
									str += "已经出库";
								}
								if(areaGroupFindBatchs[i].status == "transportation"){
									str += "已经在途";
								}
								if(areaGroupFindBatchs[i].status == "arrive"){
									str += "已达网点";
								}
								str += "</td></tr>";	
							});
							$("#shippedBatch_detailBatch_"+id).append(str);
						},
						error : function() {
						}
					});
				});
    		});
		});
		
		function storageDetailAjax(id){
			var tempString = "";
			$.ajax({
				type : 'post',
				url : '${ctx}/areaGroup/storageDetail',
				data : 'id=' + id,
				dataType : 'json',
				success : function(data) {
					$("#storageDetailBody").html("");
					var storages = data.storages;
					tempString += "<tr><th>库区名</th><th>库区类型</th></tr>";
					$.each(storages, function(i) {		
						tempString += "<tr><td>"+storages[i].name+"</td>";
						tempString += "<td>";
						if(storages[i].type == "packaging"){
							tempString += "包装仓";
						}
						if(storages[i].type == "commodity"){
							tempString += "货物仓";
						}
						if(storages[i].type == "exception"){
							tempString += "问题仓";
						}
						tempString += "</td></tr>";
					});
					//alert(tempString);
					$("#storageDetailBody").append(tempString);
				},
				error : function() {
					alert("没有找到该网点下属的库区");
				}
			});
		}
	</script>
</head>

<body>
	<c:if test="${not empty message}">
		<div id="message" class="alert alert-success"><button data-dismiss="alert" class="close">×</button>${message}</div>
	</c:if>

	<div class="row">
		<div class="span4 offset6">
			<form class="form-search" action="#">
			 	<label>网点名称：</label> <input type="text" name="search_LIKE_groupName" class="input-medium" value="${param.search_LIKE_groupName}"> 
			    <button type="submit" class="btn">搜索</button>
		    </form>
	    </div>
	</div>
	
	<div><a class="btn btn-primary" href="${ctx}/areaGroup/create">创建网点</a></div>
	
	<br/>
	
	<table id="contentTable" class="table table-striped table-bordered table-condensed">
		<thead>
			<tr>
				<th>
					<div class="dropdown pull-left">
						<a class="dropdown-toggle" data-toggle="dropdown" href="#">
				    		网点名称 <b class="caret"></b>
				    	</a>
						<ul class="dropdown-menu" >
					   		<li><a href="?sortType=groupName_desc&${searchParams}">降序</a></li>
					   		<li><a href="?sortType=groupName_asc&${searchParams}">升序</a></li>
						</ul>
					</div>
				</th>
				<th>网点编码</th>
				<th>网点信息</th>
				<th>覆盖的省份</th>
				<th>查询</th>
				<th>管理</th>
			</tr>
		</thead>
		<tbody>
		<c:forEach items="${areaGroups.content}" var="areaGroup">
			<tr>
				<td><a href="${ctx}/areaGroup/update/${areaGroup.id}">${areaGroup.groupName}</a></td>
				<td>${areaGroup.groupCode}</td>
				<td>${areaGroup.groupIntro}</td>
				<td>
					<c:forEach items="${areaGroup.provinces}" var="province">
						${province}
					</c:forEach>
				</td>
				<td>
					<%-- <a href="#area_${areaGroup.id}" data-toggle="modal" id="detail_${areaGroup.id}" name="${areaGroup.id}"  role="button" class="btn">覆盖区域</a>
 
					<div id="area_${areaGroup.id}" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
						<div class="modal-header">
					    	<h3>${areaGroup.groupName} 覆盖的区域</h3>
					  	</div>
					  	<div class="modal-body">
					  		<table class="table table-striped table-bordered table-condensed" id="areas_detail_${areaGroup.id}"></table>
						</div>
						<div class="modal-footer">
						    <button class="btn" data-dismiss="modal" aria-hidden="true">返回</button>
						</div>
					</div> --%>
				
					<a href="#pmsPipeline_${areaGroup.id}" data-toggle="modal" id="detailPipe_${areaGroup.id}" name="${areaGroup.id}"  role="button" class="btn">${areaGroup.pipelineNum} 条流水线</a>
 
					<div id="pmsPipeline_${areaGroup.id}" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
						<div class="modal-header">
					    	<h3>${areaGroup.groupName} 包含的流水线</h3>
					  	</div>
					  	<div class="modal-body">
					  		<table class="table table-striped table-bordered table-condensed" id="pmsPipelines_detailPipe_${areaGroup.id}"></table>
						</div>
						<div class="modal-footer">
						    <button class="btn" data-dismiss="modal" aria-hidden="true">返回</button>
						</div>
					</div>
					
					<a href="#shippedBatch_${areaGroup.id}" data-toggle="modal" id="detailBatch_${areaGroup.id}" name="${areaGroup.id}"  role="button" class="btn">${areaGroup.batchNum} 个批次</a>
 
					<div id="shippedBatch_${areaGroup.id}" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
						<div class="modal-header">
					    	<h3>发往 ${areaGroup.groupName} 的批次</h3>
					  	</div>
					  	<div class="modal-body">
					  		<table class="table table-striped table-bordered table-condensed" id="shippedBatch_detailBatch_${areaGroup.id}"></table>
						</div>
						<div class="modal-footer">
						    <button class="btn" data-dismiss="modal" aria-hidden="true">返回</button>
						</div>
					</div>
					
					<a href="#storageDetail" onclick="storageDetailAjax('${areaGroup.id}')" role="button" class="btn" data-toggle="modal">${areaGroup.storageNum} 个库区</a>			
				</td>
				<td>
					<a href="#delete_${areaGroup.id}" role="button" class="btn btn-danger" data-toggle="modal">删除</a>
 
					<div id="delete_${areaGroup.id}" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
						<div class="modal-body">
							<h4>确定删除${areaGroup.groupName}吗？</h4>
						</div>
						<div class="modal-footer">
							<button class="btn" data-dismiss="modal" aria-hidden="true">返回</button>
							<a class="btn btn-danger" href="${ctx}/areaGroup/delete/${areaGroup.id}">确定</a>
						</div>
					</div>
				</td>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	<tags:pagination page="${areaGroups}" paginationSize="5"/>
	
	<div id="storageDetail"  class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" role="dialog">
		<div class="modal-header">
			<h3>${areaGroup.groupName} 包含的库区</h3>
		</div>
		<div class="modal-body">
			<table id="storageDetailBody" class="table table-striped table-bordered table-condensed"></table>
		</div>
		<div class="modal-footer">
			<button class="btn" data-dismiss="modal" aria-hidden="true">返回</button>
		</div>
	</div>
</body>
</html>
